package com.itheima.reggie.controller;

import cn.hutool.core.util.StrUtil;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmplyeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmplyeeService emplyeeService;


    @GetMapping("export")
    public R exportEmployee(HttpServletResponse response){
        return emplyeeService.exportEmployee(response);
    }

    @GetMapping("/{id}")
     //修改员工数据的数据回显
    public R selectEmployeeById(@PathVariable Long id){
       return emplyeeService.updateEmployeeSelect(id);
    }
    //员工的修改
    @PutMapping
    public R updateEmployee(@RequestBody Employee employee,HttpSession session){
        return emplyeeService.updateEmployee(employee,session);

    }
     //员工分页查询
    @GetMapping("/page")
    public R pageEmployee(Integer page, Integer pageSize, String name){
    return emplyeeService.pageEmployee(page,pageSize,name);

    }
    //新增员工
    @PostMapping
    public R saveEmployee(@RequestBody Employee employee,HttpSession session){
       return emplyeeService.addEmployee(employee,session);
    }

    //员工登录
    @PostMapping("/login")
    public R<Employee> EmployeeLogin(@RequestBody Employee employee, HttpSession session){
        if (employee == null || StrUtil.hasEmpty(employee.getUsername())){
            return R.error("用户名为空");
        }
        R<Employee> r = emplyeeService.EmployeeLogin(employee,session);
        return r;
    }

    //员工登出
    @PostMapping("/logout")
    public R EmployeeLogOut(HttpSession session){
        return emplyeeService.EmployeeLogOut(session);
    }
}
